package eddy;

import java.net.URL;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;

public class HelloSound {
	
	private Clip loadSound(URL url) throws Exception {
		AudioInputStream stream = AudioSystem.getAudioInputStream(url);
		
		AudioFormat format = stream.getFormat();
		
		if ((format.getEncoding() == AudioFormat.Encoding.ULAW) || (format.getEncoding() == AudioFormat.Encoding.ALAW)) {
			AudioFormat tmp = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, format.getSampleRate(), format.getSampleSizeInBits() * 2, format.getChannels(), format.getFrameSize() * 2,
					format.getFrameRate(), true);
			stream = AudioSystem.getAudioInputStream(tmp, stream);
			format = tmp;
		}
		DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat(), ((int) stream.getFrameLength() * format.getFrameSize()));

		Clip clip = (Clip) AudioSystem.getLine(info);
//		clip.addLineListener(this);
		clip.open(stream);
		return clip;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			HelloSound hs = new HelloSound();
			Clip clip = hs.loadSound(HelloSound.class.getResource("1-welcome.wav"));
			double value = 200 / 100.0;
			FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
			float dB = (float) (Math.log(value == 0.0 ? 0.0001 : value) / Math.log(10.0) * 20.0);
			gainControl.setValue(dB);
			
			clip.start();
			new Thread().sleep(40000000);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

}
